{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Simple Mutil-Layer Percptron with Custom Data\n",
    "author hadxu(hadxu123@gmail.com)\n",
    "\"\"\"\n",
    "import os\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69 train images loaded\n",
      "18 test images loaded\n",
      "900 dimensional input\n",
      "2 classes\n"
     ]
    }
   ],
   "source": [
    "#load dataset\n",
    "cwd = os.getcwd()\n",
    "loadpath = cwd+'/data/trainingset.npz'\n",
    "l = np.load(loadpath)\n",
    "l.files\n",
    "\n",
    "# Parse data\n",
    "trainimg = l['trainimg']\n",
    "trainlabel = l['trainlabel']\n",
    "testimg = l['testimg']\n",
    "testlabel = l['testlabel']\n",
    "ntrain = trainimg.shape[0]\n",
    "nclass = trainlabel.shape[1]\n",
    "dim    = trainimg.shape[1]\n",
    "ntest  = testimg.shape[0]\n",
    "\n",
    "print (\"%d train images loaded\" % (ntrain))\n",
    "print (\"%d test images loaded\" % (ntest))\n",
    "print (\"%d dimensional input\" % (dim))\n",
    "print (\"%d classes\" % (nclass))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "#parameters\n",
    "learning_rate = 0.001\n",
    "training_epochs = 50\n",
    "batch_size = 100\n",
    "display_step = 1\n",
    "\n",
    "#network parameters\n",
    "n_hidden_1 = 16\n",
    "n_hidden_2 = 16\n",
    "n_input = 900\n",
    "n_classes = 2\n",
    "\n",
    "x = tf.placeholder('float',[None,n_input])\n",
    "y = tf.placeholder('float',[None,n_classes])\n",
    "\n",
    "#create model\n",
    "def mutilayer_perceptron(_X,_weights,_biases):\n",
    "    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X,_weights['h1']),_biases['b1']))\n",
    "    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,_weights['h2']),_biases['b2']))\n",
    "    return tf.sigmoid(tf.matmul(layer_2,_weights['out'])+_biases['out'])\n",
    "\n",
    "stddev = 0.1\n",
    "weights={\n",
    "    'h1' : tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev=stddev)),\n",
    "    'h2' : tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)),\n",
    "    'out' : tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev))\n",
    "}\n",
    "biases = {\n",
    "    'b1' : tf.Variable(tf.random_normal([n_hidden_1])),\n",
    "    'b2' : tf.Variable(tf.random_normal([n_hidden_2])),\n",
    "    'out' : tf.Variable(tf.random_normal([n_classes]))\n",
    "}\n",
    "\n",
    "#construct model\n",
    "pred = mutilayer_perceptron(x,weights,biases)\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred,y))\n",
    "optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)\n",
    "correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,'float'))\n",
    "init = tf.initialize_all_variables()\n",
    "\n",
    "print 'ok'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 000/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 001/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 002/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 003/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 004/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 005/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 006/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 007/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 008/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 009/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 010/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 011/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 012/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 013/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 014/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 015/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 016/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 017/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 018/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 019/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 020/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 021/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 022/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 023/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 024/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 025/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 026/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 027/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 028/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 029/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 030/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 031/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 032/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 033/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 034/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 035/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 036/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 037/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 038/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 039/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 040/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 041/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 042/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 043/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 044/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 045/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 046/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 047/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 048/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Epoch: 049/050 cost: 0.000000000\n",
      "Training accuracy: 0.722\n",
      "Optimization Finished!\n"
     ]
    }
   ],
   "source": [
    "# Launch the graph\n",
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "\n",
    "# Training cycle\n",
    "for epoch in range(training_epochs):\n",
    "    avg_cost = 0.\n",
    "    total_batch = int(ntrain/batch_size)\n",
    "    # Loop over all batches\n",
    "    for i in range(total_batch):\n",
    "        randidx = np.random.randint(ntrain,size=batch_size)\n",
    "        batch_xs = trainimg[randidx,:]\n",
    "        batch_ys = trainlabel[randidx,:]\n",
    "        # Fit training using batch data\n",
    "        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})\n",
    "        # Compute average loss\n",
    "        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch\n",
    "    # Display logs per epoch step\n",
    "    if epoch % display_step == 0:\n",
    "        print (\"Epoch: %03d/%03d cost: %.9f\" % (epoch, training_epochs, avg_cost))\n",
    "        train_acc = sess.run(accuracy, feed_dict={x: testimg, y: testlabel})\n",
    "        print (\"Training accuracy: %.3f\" % (train_acc))\n",
    "\n",
    "print (\"Optimization Finished!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
